import socket
from concurrent.futures import ThreadPoolExecutor


def scan_ip(ip, port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        sock.settimeout(0.1)  # 设置超时
        result = sock.connect_ex((ip, port))  # 连接到指定的IP和端口
        if result == 0:
            return ip, port
    except socket.timeout:
        pass
    finally:
        sock.close()


def broadcast_scan(ip_prefix, port):
    tasks = []
    success = []
    with ThreadPoolExecutor(max_workers=100) as executor:
        for i in range(1, 255):
            ip = ip_prefix + str(i)
            tasks.append(executor.submit(scan_ip, ip, port))

    for task in tasks:
        status = task.result()
        if status:
            success.append(status)
    if success:
        return success[0]
    return None, None

def main():
    host, _ = broadcast_scan(ip_prefix, int(port))
    print('扫描结果：', host)